package com.guanghua.brick.html;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.guanghua.brick.html.tag.ContentConfigTag;

public class ContentXMLServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ContentXMLServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/xml");
		response.setHeader("Cache-Control", "no-cache");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(buildContent(request, response));
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/xml");
		response.setHeader("Cache-Control", "no-cache");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(buildContent(request, response));
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}
	
	/**
	 * 构造content 的xml的方法
	 * @param request
	 * @param response
	 * @return
	 */
	private String buildContent(HttpServletRequest request, HttpServletResponse response) {
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
		sb.append("<objects>\n");
		
		String id = request.getParameter("content");
		if (id != null && id.length() != 0) {
			Map<String, IContent> pool = ContentConfigTag.getContentPool(this.getServletContext());
			if (pool != null) {
				IContent content = pool.get(id);
				if (content != null) {
					List<Map<String, String>> list = content.buildContent(request, response);
					Iterator<Map<String, String>> i = list.iterator();
					while (i.hasNext()) {
						Map<String, String> map = i.next();
						Iterator<String> j = map.keySet().iterator();
						sb.append("	<object>\n");
						while (j.hasNext()) {
							String key = j.next();
							sb.append("		<property name=\"")
							.append(key)
							.append("\">")
							.append(map.get(key))
							.append("</property>\n");
						}
						sb.append("	</object>\n");
					}
				}
			}
		}
		
		sb.append("</objects>\n");
		return sb.toString();
	}

}
